﻿<template>
  <el-dialog
    title="批量设置"
    v-model="visible"
    :width="1000"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-container>
      <el-main class="nopadding">
        <el-table
          ref="myBatchTable"
          :data="batchList"
          style="width: 100%"
          @selection-change="selectionChange"
          height="560"
        >
          <el-table-column type="selection" width="50"></el-table-column>
          <el-table-column
            label="序号"
            prop="rownumber"
            width="50"
          ></el-table-column>
          <el-table-column
            v-if="false"
            label="字段"
            prop="Code"
            width="120"
          ></el-table-column>
          <el-table-column
            label="字段"
            prop="Name"
            width="120"
          ></el-table-column>
          <el-table-column
            label="数据类型"
            prop="DataType"
            width="120"
          ></el-table-column>
          <el-table-column prop="Value" label="内容" width="300">
            <template #default="scope">
              <template v-if="scope.row.DataType == 'string'">
                <el-input v-model="scope.row.Value" />
              </template>
              <template v-else-if="scope.row.DataType == 'bool'">
                <el-select style="width: 275px" v-model="scope.row.Value">
                  <el-option label="---请选择---" value="" />
                  <el-option label="True" value="True" />
                  <el-option label="False" value="False" />
                  <el-option label="~" value="~" />
                </el-select>
              </template>
              <template v-if="scope.row.DataType == 'number'">
                <el-checkbox
                  v-model="scope.row.ExendState"
                  @click="scope.row.ExendValue = ''"
                  >扩展</el-checkbox
                >
                <el-input-number
                  style="width: 225px"
                  v-model="scope.row.Value"
                  :disabled="scope.row.ExendState"
                />
              </template>
              <template v-else-if="scope.row.DataType == 'datetime'">
                <el-checkbox
                  v-model="scope.row.ExendState"
                  @click="scope.row.ExendValue = ''"
                  >扩展</el-checkbox
                >
                <el-date-picker
                  v-if="scope.row.DataFormat == 'YYYY-MM-DD'"
                  style="width: 225px"
                  v-model="scope.row.Value"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="---请选择---"
                  :disabled="scope.row.ExendState"
                />
                <el-time-picker
                  v-else-if="scope.row.DataFormat == 'HH:mm:ss'"
                  style="width: 225px"
                  v-model="scope.row.Value"
                  placeholder="---请选择---"
                  :disabled="scope.row.ExendState"
                />
                <el-date-picker
                  v-else
                  style="width: 225px"
                  v-model="scope.row.Value"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="---请选择---"
                  :disabled="scope.row.ExendState"
                />
              </template>
              <template v-else-if="scope.row.DataType == 'array'">
                <el-select style="width: 275px" v-model="scope.row.Value">
                  <el-option label="---请选择---" value="" />
                  <el-option
                    v-for="item in scope.row.Source"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </template>
              <template v-else-if="scope.row.DataType == 'dictionary'">
                <el-select style="width: 275px" v-model="scope.row.Value">
                  <el-option label="---请选择---" value="" />
                  <el-option
                    v-for="item in scope.row.Source"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </template>
              <template v-else-if="scope.row.DataType == 'josn'">
                <el-cascader
                  style="width: 275px"
                  :props="props1"
                  :show-all-levels="false"
                  :emitPath="false"
                  placeholder="---请选择---"
                  v-model="scope.row.Value"
                  :options="scope.row.Source"
                />
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="ExendValue" label="扩展" width="300">
            <template #default="scope">
              <template v-if="scope.row.DataType == 'string'">
                <el-radio-group v-model="scope.row.ExendType" class="ml-4">
                  <el-radio label="cover" size="large">覆盖</el-radio>
                  <el-radio label="append" size="large">追加</el-radio>
                </el-radio-group>
              </template>
              <template v-else-if="scope.row.DataType == 'bool'"> </template>
              <template
                v-else-if="
                  scope.row.DataType == 'number' && scope.row.ExendState
                "
              >
                <el-select style="width: 80px" v-model="scope.row.ExendSign">
                  <el-option label="" value="" />
                  <el-option label="+" value="+" />
                  <el-option label="-" value="-" />
                  <el-option label="*" value="*" />
                  <el-option label="/" value="/" />
                </el-select>
                <el-input
                  style="width: 80px"
                  v-show="scope.row.ExendState"
                  v-model="scope.row.ExendValue"
                />
                <el-checkbox v-model="scope.row.Exend1" label="%" />
                <el-checkbox v-model="scope.row.Exend2" label="取整" />
              </template>
              <template
                v-else-if="
                  scope.row.DataType == 'dateTime' && scope.row.ExendState
                "
              >
                <el-select style="width: 80px" v-model="scope.row.ExendSign">
                  <el-option label="+" value="+" />
                  <el-option label="-" value="-" />
                </el-select>
                <el-input
                  style="width: 80px"
                  v-show="scope.row.ExendState"
                  v-model="scope.row.ExendValue"
                />
                <el-select
                  v-if="scope.row.DataFormat == 'YYYY-MM-DD'"
                  style="width: 80px"
                  v-model="scope.row.Exend3"
                >
                  <el-option label="年" value="yy" />
                  <el-option label="月" value="mo" />
                  <el-option label="日" value="da" />
                </el-select>
                <el-select
                  v-else-if="scope.row.DataFormat == 'HH:mm:ss'"
                  style="width: 80px"
                  v-model="scope.row.Exend3"
                >
                  <el-option label="时" value="ho" />
                  <el-option label="分" value="mi" />
                  <el-option label="秒" value="se" />
                </el-select>
                <el-select
                  v-else
                  style="width: 80px"
                  v-model="scope.row.Exend3"
                >
                  <el-option label="年" value="yy" />
                  <el-option label="月" value="mo" />
                  <el-option label="日" value="da" />
                  <el-option label="时" value="ho" />
                  <el-option label="分" value="mi" />
                  <el-option label="秒" value="se" />
                </el-select>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        :icon="Save"
         
        type="primary"
        @click="submit"
      >
        保存
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import publicjs from "@/api/model/publicjs";
export default {
  data() {
    return {
      visible: false,
      isSaveing: false,
      selectionRows: [],
      batchList: [],
    };
  },
  mounted() {
    this.getFieldsData();
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单提交方法
    submit() {
      this.$nextTick(() => {
        if (this.selectionRows.length > 0) {
          // eslint-disable-next-line no-debugger
        //debugger;
          let resultList = [];
          this.selectionRows.forEach((element) => {
            let data = new publicjs.resultData();
            switch (element.DataType) {
              case "string":
              case "array":
              case "dictionary":
              case "josn":
              case "bool":
                data.Code = element.Code;
                data.DataType = element.DataType;
                data.Value = element.Value;
                data.ExendType = element.ExendType;
                break;
              case "number":
              case "dateTime":
                data.Code = element.Code;
                data.DataType = element.DataType;
                data.DataFormat = element.DataFormat;
                data.Value = element.Value;
                data.ExendType = element.ExendType;
                data.ExendState = element.ExendState;
                data.ExendValue = element.ExendValue;
                data.ExendSign = element.ExendSign;
                data.Exend1 = element.Exend1;
                data.Exend2 = element.Exend2;
                data.Exend3 = element.Exend3;
                break;
            }
            resultList.push(data);
          });
          this.$emit("fatherMethod", resultList);
        } else this.$message.success("请选择记录");
      });
    },
    selectionChange(selection) {
      // eslint-disable-next-line no-debugger
      //debugger;
      this.selectionRows = selection;
    },
    async getFieldsData() {
      var head = new publicjs.head();
      publicjs.initHead(head, "batchstruct");
      var ed = new publicjs.entitydata(head);
      var eo = new publicjs.entityobj();
      eo.AttrList = {
        Name: "UserInfo",
        UseDistinct: false,
        OrgCode: "",
        UseBackUp: false,
        UseGroup: false,
      };
      eo.ValueList = {
        Code: "Code",
        Name: "Name",
        OrgName: "OrgName",
        DepartCode: "DepartCode",
        SexCode: "SexCode",
        DutyCode: "DutyCode",
        Mobile: "Mobile",
        Email: "Email",
        WeChat: "WeChat",
        BirthDate: "BirthDate",
        IdNumber: "IdNumber",
        IsCustomerMan:"IsCustomerMan",
        AllowOrder:"AllowOrder",
        State: "State",
      };
      ed.Data.Content.ObjectList.Object.push(eo);
      try {
        var returnValueStr = await this.$API.auth.objProcess.post(ed);
        //console.log(returnValueStr);
        var returnValue = JSON.parse(returnValueStr);
        // eslint-disable-next-line no-debugger
      //debugger;
        if (returnValue.Result.OpFlag == "1") {
          var objectList =
            returnValue.Result.Content.ObjectList.Object.rows.row;

          for (var i = 0; i < objectList.length; i++) {
            if (objectList[i].Source && objectList[i].Source != "null")
              objectList[i].Source = eval("(" + objectList[i].Source + ")");
            if (objectList[i].ExendState.toLowerCase() == "true")
              objectList[i].ExendState = true;
            else if (objectList[i].ExendState.toLowerCase() == "false")
              objectList[i].ExendState = false;
          }
          this.batchList = objectList;
        }
      } catch (error) {
        return [];
      }
    },
  },
};
</script>

<style></style>
